package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;

import com.itheima.pojo.CheckItem;
import com.itheima.pojo.FoodsInfo;
import com.itheima.pojo.OrderSetting;
import com.itheima.pojo.SportsInfo;
import com.itheima.service.KnowledgeBaseService;
import com.itheima.utils.POIUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/knowledge")
public class KnowledgeBaseController {

    @Reference
    private KnowledgeBaseService knowledgeBaseService;

    @RequestMapping("/findPage1")
    public PageResult findPage1(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = knowledgeBaseService.pageQuery(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return pageResult;
    }
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = knowledgeBaseService.pageQuery1(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return pageResult;
    }


    @RequestMapping("/upload2")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile) {

        try {
            //读取Excel文件数据
            List<String[]> list = POIUtils.readExcel(excelFile);
            if (list != null && list.size() > 0) {
                List<FoodsInfo> foodsInfosList = new ArrayList<>();
                for (String[] strings : list) {
                    FoodsInfo foodsInfo = new FoodsInfo(
                            Integer.parseInt(strings[1]),
                            strings[2],
                            Integer.parseInt(strings[3]),
                            strings[4],
                            Integer.parseInt(strings[5]),
                            Double.parseDouble(strings[6]),
                            Integer.parseInt(strings[7]),
                            Integer.parseInt(strings[8]),
                            Double.parseDouble(strings[9]),
                            Double.parseDouble(strings[10]),
                            Double.parseDouble(strings[11]),
                            Integer.parseInt(strings[12]),
                            Integer.parseInt(strings[13]),
                            Integer.parseInt(strings[14]),
                            Integer.parseInt(strings[15]),
                            Integer.parseInt(strings[16]),
                            Integer.parseInt(strings[17]),
                            Integer.parseInt(strings[18]),
                            Integer.parseInt(strings[19]),
                            Integer.parseInt(strings[20]),
                            Integer.parseInt(strings[21]),
                            strings[22],
                            Integer.parseInt(strings[23]),
                            Integer.parseInt(strings[24]),
                            Integer.parseInt(strings[25]));
                    foodsInfosList.add(foodsInfo);
                }
                knowledgeBaseService.addFoodInfo(foodsInfosList);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
        return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
    }

    @RequestMapping("/upload1")
    public Result upload1(@RequestParam("excelFile") MultipartFile excelFile) {

        try {
            //读取Excel文件数据
            List<String[]> list = POIUtils.readExcel(excelFile);
            if (list != null && list.size() > 0) {
                List<SportsInfo> sportsInfosList = new ArrayList<>();
                for (String[] strings : list) {
                    SportsInfo sportsInfo  = new SportsInfo(
                            strings[1],
                            strings[2],
                            Integer.parseInt(strings[3]),
                            Integer.parseInt(strings[4]),
                            strings[5],
                            strings[6],
                            Integer.parseInt(strings[7]),
                            Integer.parseInt(strings[8]),
                            Double.parseDouble(strings[9]),
                            Double.parseDouble(strings[10]),
                            Double.parseDouble(strings[11]),
                            Integer.parseInt(strings[12]),
                            Integer.parseInt(strings[13]),
                            strings[14],
                            strings[15],
                            Integer.parseInt(strings[16]),
                            Double.parseDouble(strings[17]),
                            Double.parseDouble(strings[18]));
                    sportsInfosList.add(sportsInfo);
                }
                knowledgeBaseService.addSportInfo(sportsInfosList);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
        return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
    }

    @RequestMapping("/findFoodById")
    public Result findFoodById(Integer id){
        try{
            FoodsInfo foodsInfo = knowledgeBaseService.findFoodById(id);
            return new Result(true, MessageConstant.QUERY_KNOWLEDGE_SUCCESS,foodsInfo);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.QUERY_KNOWLEDGE_FAIL);
        }
    }

    @RequestMapping("/findSportById")
    public Result selectSportById(Integer id){
        try{
            SportsInfo sportsInfo = knowledgeBaseService.selectSportById(id);
            return new Result(true, MessageConstant.QUERY_KNOWLEDGE_SUCCESS,sportsInfo);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.QUERY_KNOWLEDGE_FAIL);
        }
    }


    //编辑
    @RequestMapping("/edit")
    public Result edit(@RequestBody FoodsInfo foodsInfo){
        try {
            knowledgeBaseService.edit(foodsInfo);
        }catch (Exception e){
            return new Result(false,MessageConstant.EDIT_KNOWLEDGE_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_KNOWLEDGE_SUCCESS);
    }
    //编辑
    @RequestMapping("/editSport")
    public Result editSport(@RequestBody SportsInfo sportsInfo){
        try {
            knowledgeBaseService.editSport(sportsInfo);
        }catch (Exception e){
            return new Result(false,MessageConstant.EDIT_KNOWLEDGE_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_KNOWLEDGE_SUCCESS);
    }


    //删除
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            knowledgeBaseService.delete(id);
        }catch (RuntimeException e){
            return new Result(false,e.getMessage());
        }catch (Exception e){
            return new Result(false, MessageConstant.DELETE_KNOWLEDGE_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_KNOWLEDGE_SUCCESS);
    }

    //删除
    @RequestMapping("/deleteSport")
    public Result deleteSport(Integer id){
        try {
            knowledgeBaseService.deleteSport(id);
        }catch (RuntimeException e){
            return new Result(false,e.getMessage());
        }catch (Exception e){
            return new Result(false, MessageConstant.DELETE_KNOWLEDGE_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_KNOWLEDGE_SUCCESS);
    }

    //新增
    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")//权限校验
    public Result add(@RequestBody FoodsInfo foodsInfo){
        try {
            knowledgeBaseService.add(foodsInfo);
        }catch (Exception e){
            return new Result(false, MessageConstant.ADD_KNOWLEDGE_FAIL);
        }
        return new Result(true,MessageConstant.ADD_KNOWLEDGE_SUCCESS);
    }



    //新增
    @RequestMapping("/addSport")
    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")//权限校验
    public Result addSport(@RequestBody SportsInfo sportsInfo){
        try {
            knowledgeBaseService.addSport(sportsInfo);
        }catch (Exception e){
            return new Result(false, MessageConstant.ADD_KNOWLEDGE_FAIL);
        }
        return new Result(true,MessageConstant.ADD_KNOWLEDGE_SUCCESS);
    }
}
